package com.yyx.app1.MedicationAssistant_function.UI;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yyx.app1.CommonControls.MyViewPager.BackButton;
import com.yyx.app1.Data.DataInit;
import com.yyx.app1.MedicationAssistant_function.Controls.SearchDrup_Activity_Control.HotSearch_ListView.HotSearch_ListView_Adapter;
import com.yyx.app1.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.HashMap;
import java.util.List;

/**
 * Created by yuexing on 2015/9/7.
 */
@EActivity(R.layout.activity_searchmedication)
public class SearchMedication_Activity extends Activity{
    private HotSearch_ListView_Adapter adapter=null;
    private DataInit dataInit;
    @ViewById
    TextView search_clear_button,search_title;

    @ViewById
    EditText search_edittext;

    @ViewById
    ListView searchdrup_listview;

    @ViewById
    Button search_searchbutton;

    @StringRes
    String hotsearch;

    @Click
    public void home()
    {
        finish();
    }

    @Click
    public void search_searchbutton()
    {
        try {
            String str = search_edittext.getText().toString();
            if (str.isEmpty())
            {
                Toast.makeText(this,"请输入药品名称",Toast.LENGTH_LONG).show();
                return;
            }
            List<HashMap<String,String>> list = dataInit.getMedication_SQLiteOperation().fuzzySearch(str);
            changeAdapter(list);
            search_title.setText("搜索结果:   \""+search_edittext.getText().toString()+"\"找到"+list.size()+"个结果");
        }
        catch (Exception e)
        {
            Log.e("异常1510140921",e.toString());
        }
    }

    @Click
    public void search_edittext()
    {

    }

    @Click
    public void search_clear_button()
    {
        search_clear_button.setTextColor(Color.BLACK);
        search_edittext.setText("");
        new Handler().postDelayed(delete_button_chang, 300);
        search_title.setText(hotsearch);
        List list = dataInit.getMedication_SQLiteOperation().getDemo_10nameInfo();
        changeAdapter(list);
    }

    @ItemClick(R.id.searchdrup_listview)
    public void listviewclick(int position)
    {
        String  str = search_title.getText().toString();
        if (str.equals(hotsearch))
        {
            search_edittext.setText(((HashMap<String, String>) adapter.getItem(position)).get("medicineName"));
        }
        else
        {
            Intent intent =new Intent(this,MedicationInfo_Activity_.class);
            intent.putExtra("medication", ((HashMap<String, String>) adapter.getItem(position)).get("medicineName"));
            startActivity(intent);
        }
    }

    @AfterViews
    public void init()
    {
        dataInit = new DataInit(this,false,"cxlin");
        List list = dataInit.getMedication_SQLiteOperation().getDemo_10nameInfo();
        adapter = new HotSearch_ListView_Adapter(this);
        changeAdapter(list);
        searchdrup_listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
        searchdrup_listview.setDivider(new ColorDrawable(Color.rgb(200,200,200)));
        searchdrup_listview.setDividerHeight(1);
    }

    Runnable delete_button_chang = new Runnable() {
        @Override
        public void run() {
            search_clear_button.setTextColor(Color.rgb(255,255,255));
        }
    };

    public void changeAdapter(List list)
    {
        adapter.findAll(list);
        searchdrup_listview.setAdapter(adapter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            home();
        }
        return true;
    }
}
